package com.umbrella.manage.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

/**
 *
 *
 * @author Wan, ChuanLong @ 07/02/2018
 */
public class FileUtils {

	/**
	 * 
	 * @param dirctory:保存文件的目录
	 * @param fileName:文件名称
	 * @param content:文件内容
	 */
	public static void saveFile(String dirctory, String fileName, byte[] content) {

		File file = new File(dirctory);
		if (!file.exists()) {

			file.mkdirs();
		}
		
		FileOutputStream outputStream = null;
		BufferedOutputStream buffer = null;
		try {
			
			outputStream = new FileOutputStream(new File(dirctory + File.separator + fileName));
			buffer = new BufferedOutputStream(outputStream);
			buffer.write(content);
			buffer.flush();
		} catch (Exception e) {

		}finally {
			
			if(outputStream != null) {
				try {
					
					outputStream.close();
				} catch (IOException e) {
				}
			}
			
			if(buffer != null) {
				try {
					
					buffer.close();
				} catch (IOException e) {
				}
			}
		}
	}
	
	public static void deleteFiles(File file) {

		if (!file.exists()) {

			return;
		}

		if (file.isDirectory()) {// 如果是目录，先递归删除
			File[] list = file.listFiles();
			for (File temp : list) {
				deleteFiles(temp);// 先删除目录下的文件
			}
		}

		file.delete();
	}

	public static void unzip(String zipFile, String targetDir) throws Exception {// storage/sdcard0/Download/CarExam_1474453235684.zip

		BufferedInputStream bi = null;
		BufferedOutputStream bos = null;
		try {

			ZipFile zf = new ZipFile(zipFile, "UTF-8");// 支持中文
			Enumeration<?> e = zf.getEntries();
			while (e.hasMoreElements()) {
				ZipEntry ze2 = (ZipEntry) e.nextElement();
				String entryName = ze2.getName();

				String path = targetDir + "/" + entryName;
				if (ze2.isDirectory()) {
					// System.out.println("正在创建解压目录: " + entryName);
					File decompressDirFile = new File(path);
					if (!decompressDirFile.exists()) {
						decompressDirFile.mkdirs();
					}
				} else {
					// System.out.println("正在创建解压文件:" + entryName);
					String fileDir = path.substring(0, path.lastIndexOf("/"));
					File fileDirFile = new File(fileDir);
					if (!fileDirFile.exists()) {
						fileDirFile.mkdirs();
					}
					bos = new BufferedOutputStream(new FileOutputStream(targetDir + "/" + entryName));

					bi = new BufferedInputStream(zf.getInputStream(ze2));
					byte[] readContent = new byte[4096];
					int readCount = bi.read(readContent);
					while (readCount != -1) {
						bos.write(readContent, 0, readCount);
						readCount = bi.read(readContent);
						bos.flush();
					}
					
					bos.close();
					
					bi.close();

				}
			}
			zf.close();
		} finally {

			if (bos != null) {

				bos.close();
			}
			if (bi != null) {

				bi.close();
			}
		}
	}
}
